package com.lee.base;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.lee.base.model.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

/**
 * @Description TODO
 * @Author winston
 * @DateTime 2022/3/9
 */
@SpringBootTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class DocBachOperation {

    private static RestHighLevelClient client;
    private static ObjectMapper objectMapper;
    @BeforeAll
    public static void init() {
        client = new RestHighLevelClient(RestClient.builder(HttpHost.create("https://elasticsearch-dev.souban.io")));
        objectMapper = new ObjectMapper();
    }
    @Test
    @Order(1)
    public void insert() throws IOException {
        User user1 = User.builder() .name("小米").gender("男").age(10).build();
        User user2 = User.builder().name("华为").gender("女").age(30).build();
        User user3 = User.builder().name("苹果").gender("男").age(20).build();
        User user4 = User.builder().name("荣耀").gender("女").age(10).build();
        User user5 = User.builder().name("中兴").gender("男").age(1).build();
        User user6 = User.builder().name("vivo").gender("女").age(4).build();
        User user7 = User.builder().name("oppo").gender("男").age(40).build();
        String user1Json = objectMapper.writeValueAsString(user1);
        String user2Json = objectMapper.writeValueAsString(user2);
        String user3Json = objectMapper.writeValueAsString(user3);
        String user4Json = objectMapper.writeValueAsString(user4);
        String user5Json = objectMapper.writeValueAsString(user5);
        String user6Json = objectMapper.writeValueAsString(user6);
        String user7Json = objectMapper.writeValueAsString(user7);
        IndexRequest request1 = new IndexRequest("user").id("1001").source(user1Json, XContentType.JSON);
        IndexRequest request2 = new IndexRequest("user").id("1002").source(user2Json, XContentType.JSON);
        IndexRequest request3 = new IndexRequest("user").id("1003").source(user3Json, XContentType.JSON);
        IndexRequest request4 = new IndexRequest("user").id("1004").source(user4Json, XContentType.JSON);
        IndexRequest request5 = new IndexRequest("user").id("1005").source(user5Json, XContentType.JSON);
        IndexRequest request6 = new IndexRequest("user").id("1006").source(user6Json, XContentType.JSON);
        IndexRequest request7 = new IndexRequest("user").id("1007").source(user7Json, XContentType.JSON);
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(request1);
        bulkRequest.add(request2);
        bulkRequest.add(request3);
        bulkRequest.add(request4);
        bulkRequest.add(request5);
        bulkRequest.add(request6);
        bulkRequest.add(request7);

        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.getItems());
    }



    @Test
    @Order(2)
    public void delete() throws IOException {
        DeleteRequest request1 = new DeleteRequest("user").id("1001");
        DeleteRequest request2 = new DeleteRequest("user").id("1002");
        DeleteRequest request3 = new DeleteRequest("user").id("1003");
        BulkRequest bulkRequest = new BulkRequest();
        bulkRequest.add(request1);
        bulkRequest.add(request2);
        bulkRequest.add(request3);

        BulkResponse bulkResponse = client.bulk(bulkRequest, RequestOptions.DEFAULT);
        System.out.println(bulkResponse.getItems());
    }

    @AfterAll
    private static void destory() throws IOException {
        client.close();
    }
}
